import java.util.Arrays;
import java.util.Scanner;

//文字格斗游戏
public class Game {
    public static void main(String[] args) {
        //创建一个玩家角色
        Role role1 = creatRole();
        //展示玩家角色
        showRole(role1);
        //输赢判断跳出循环
        int flag = 1;
        //用于添加获得的技能，同时使其不会溢出技能数组
        int count = 0;

        while(flag == 1){
            //创建一个人机敌对角色
            Role role2 = creatOpponentRole(role1);
            //根据玩家的技能获得情况获取技能
            for (int i = 0; i < count; i++) {
                role2.setKungFuRandom();
            }
            //展示敌对角色
            showRole(role2);
            //开始攻击
            while (true){
                //回合制，玩家放先攻击敌对方
                role1.attack(role2);
                //根据血量判断情况
                if(role2.getBlood() == 0){
                    //敌对方血量为0，我方获胜，玩家角色血量重新补充至100
                    System.out.printf( "%s K.O %s",role1.getName(),role2.getName());
                    System.out.println();
                    System.out.println("you are winner");
                    role1.setBlood(100);
                    //可获得的技能+1
                    if(count < 3){
                        count++;
                    }
                    flag = 1;
                    break;
                }
                role2.attack(role1);
                if(role1.getBlood() == 0){
                    System.out.printf( "%s K.O %s",role2.getName(),role1.getName());
                    System.out.println();
                    System.out.println("you are loser");
                    //敌对方获胜，跳出循环
                    flag = 0;
                    break;
                }
                System.out.println();
            }
            //玩家获胜时，询问玩家是否继续游戏，如果继续，技能数组没满就可继续获得技能
            //否则玩家退出游戏，结束
            if(flag == 1){
                Scanner scanner = new Scanner(System.in);
                while (true){
                    showRole(role1);
                    System.out.println("是否继续游戏(yes or no):");
                    String s = scanner.nextLine();
                    if(s.equalsIgnoreCase("yes")){
                        if(count <= 3){
                            System.out.println("你学习了新的功夫！！！");
                            role1.setKungFuRandom();
                        }
                        showRole(role1);
                        break;
                    } else if (s.equalsIgnoreCase("no")) {
                        flag = 0;
                        break;
                    }else {
                        System.out.println("请重新输入");
                    }
                }
            }
        }

    }

    //生成角色，可随机生成名字和自行输入名字
    public static Role creatRole(){
        System.out.println("创建角色中");
        Role role = new Role();
        Scanner scanner = new Scanner(System.in);
        while (true){
            System.out.println("是否自行输入姓名(yes or no):");
            String s = scanner.nextLine();
            if(s.equalsIgnoreCase("yes")){
                System.out.println("请输入姓名");
                role.setName(scanner.nextLine());
                break;
            } else if (s.equalsIgnoreCase("no")) {
                role.setNameRandom();
                break;
            }else {
                System.out.println("请重新输入");
            }
        }
        while (true) {
            System.out.println("请输入性别（男 1/ 女 2）");
            int a = scanner.nextInt();
            if (a == 1) {
                role.setGender('男');
                break;
            } else if (a == 2) {
                role.setGender('女');
                break;
            } else {
                System.out.println("请重新输入");
            }
        }
        role.setAge();
        role.setFace();
        role.setKungFu();
        return role;
    }

    //生成敌对角色
    public static Role creatOpponentRole(Role role1){
        System.out.println("生成敌对角色中");
        Role role = new Role();
        while (true){
            role.setNameRandom();
            if(!role.getName().equals(role1.getName())){
                break;
            }
        }
        role.setGender('男');
        role.setAge();
        role.setFace();
        role.setKungFu();
        return role;
    }

    //展示角色面板
    public static void showRole(Role role){
        System.out.println("姓名：" + role.getName());
        System.out.println("性别：" + role.getGender());
        System.out.println("年龄：" + role.getAge());
        System.out.println("长相：" + role.getFace());
        System.out.println("血量：" + role.getBlood());
        System.out.println("拥有功夫：" + Arrays.toString(role.getKungFu()));
    }
}
